﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using TDT_Project.classes;
using Telerik.Web.UI;
namespace TDT_Project.Controller
{
    public partial class XemDiemPhuHuynh : System.Web.UI.UserControl
    {
        cls_phuhuynhxemdiem cls = new cls_phuhuynhxemdiem();
        cls_lop l = new cls_lop();
        public string g_canam = "";
        public string g_hocluc = "";
        public string g_thilai = "";
        public string g_ketquarenluyen = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Session["hs"] != null && Session["nk"] != null && Session["hk"] != null && Session["lop"] != null && Convert.ToDecimal(Session["Loai"]) == 0)
                    {

                        string hocsinh = Session["hs"].ToString();
                        string nienkhoa = Session["nk"].ToString();
                        string hocky = Session["hk"].ToString();
                        string lop = Session["lop"].ToString();
                        gvResult.DataSource = cls.Get_Diem_HS(hocky, nienkhoa, hocsinh, lop).Tables[0].DefaultView;
                        gvResult.DataBind();
                        DataSet dsGV = l.Load_gvien_lop_quoc(nienkhoa, lop);
                        if (dsGV != null && dsGV.Tables[0].Rows.Count > 0)
                        {

                            lbl_gv.Text = "<b>" + dsGV.Tables[0].Rows[0]["TenGiaoVien"].ToString() + "</b>";
                            lbl_sdt.Text = "<b>" + dsGV.Tables[0].Rows[0]["DienThoai"].ToString() + "</b>";
                        }
                        //load thong tin hoc sinh
                        DataSet ds = cls.Get_ThongTin_HS(lop, nienkhoa, hocsinh);
                        if (ds != null && ds.Tables.Count > 0)
                        {
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                lbl_hoten.Text = ds.Tables[0].Rows[0]["HoTen"].ToString();
                                lblnamsinh.Text = ds.Tables[0].Rows[0]["NgaySinh"].ToString();
                                lbldiachi.Text = ds.Tables[0].Rows[0]["DiaChi"].ToString();
                                lblnoisinh.Text = ds.Tables[0].Rows[0]["NoiSinh"].ToString();
                                lbllop.Text = ds.Tables[0].Rows[0]["Lop"].ToString();
                                lblnamnhaphoc.Text = ds.Tables[0].Rows[0]["NgayVaoTruong"].ToString();
                                lbl_stt.Text = ds.Tables[0].Rows[0]["STT"].ToString();
                                lbl_sbd.Text = ds.Tables[0].Rows[0]["SOBaoDanh"].ToString();
                                lblphongthi.Text = ds.Tables[0].Rows[0]["STTPhongThi"].ToString();
                                Session["Khoi"] = ds.Tables[0].Rows[0]["Khoi_ID"].ToString();
                                // lbl_vipham.Text = ds.Tables[0].Rows[0]["Nghi"].ToString();
                            }
                        }
                        //end
                        //load hoc luc hoc sinh
                        DataSet ds_hocluc = cls.Get_HOCLUC_HS(lop, nienkhoa, hocsinh, hocky);
                        if (ds_hocluc != null && ds_hocluc.Tables.Count > 0)
                        {
                            if (ds_hocluc.Tables[0].Rows.Count > 0)
                            {
                                //lblDTB.Text = "DTB: " + ds_hocluc.Tables[0].Rows[0]["DiemTB"].ToString();
                                //lbl_hang.Text = "Hạng: " + ds_hocluc.Tables[0].Rows[0]["Hang"].ToString();
                                //lbl_dntd.Text = "DHTĐ: " + ds_hocluc.Tables[0].Rows[0]["DanhHieuThiDua"].ToString();
                                //lbl_loai.Text = "Học lực: " + ds_hocluc.Tables[0].Rows[0]["hocluc"].ToString();
                                //lblhk.Text =ds_hocluc.Tables[0].Rows[0]["hanhkiem"].ToString();
                                //lbl_nghi.Text = ds_hocluc.Tables[0].Rows[0]["Nghi"].ToString();
                                lblhk.Text = ds_hocluc.Tables[0].Rows[0]["hanhkiem"].ToString();
                                lbl_nghi.Text = ds_hocluc.Tables[0].Rows[0]["Nghi"].ToString();
                                lbl_nhanxet.Text = ds_hocluc.Tables[0].Rows[0]["Nhanxet"].ToString();
                                g_hocluc += "<table style=\"background: #2119B2; width: 520px;\"> <tr>";
                                g_hocluc += " <td style=\"width:60px;color:White\">" + "ĐTB: " + ds_hocluc.Tables[0].Rows[0]["DiemTB"].ToString() + "</td>";
                                g_hocluc += " <td style=\"width:100px;color:White\">" + "Học lực: " + ds_hocluc.Tables[0].Rows[0]["hocluc"].ToString() + "</td>";
                                g_hocluc += " <td style=\"width:60px;color:White\">" + "Hạng: " + ds_hocluc.Tables[0].Rows[0]["Hang"].ToString() + "</td>";
                                g_hocluc += " <td style=\"width:80px;color:White\">" + "DHTĐ: " + ds_hocluc.Tables[0].Rows[0]["DanhHieuThiDua"].ToString() + "</td>";
                                g_hocluc += "</tr></table>";
                            }
                        }
                        //end
                        //load nhan xet GV
                        // GetNhanXet_GV();
                        //load PH
                        //  GetNhanXet_PH();
                        // DataTable dt = new DataTable();
                        //rid_KQ.DataSource = dt;
                        // rid_KQ.DataBind();
                        // load vi pham
                        // Get_ViPham(hocky);
                    }

                    else
                    {
                        Response.Redirect("Default.aspx");
                    }

                }
                catch (Exception)
                {
                    throw;
                }

            }
        }
        private void Get_ViPham(string hocky)
        {
            if (Session["hs"] != null && Session["nk"] != null && Session["hk"] != null && Session["lop"] != null)
            {
                DataSet ds = cls.Get_ViPham(Session["hk"].ToString(), hocky, Session["hs"].ToString());
                if (ds != null && ds.Tables.Count > 0)
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                            lbl_vipham.Text += ds.Tables[0].Rows[i]["TenKhenThuongKyLuat"].ToString() + " (" + ds.Tables[0].Rows[i]["NgayVP"].ToString() + " " + ds.Tables[0].Rows[i]["TenMonHoc"].ToString() + ")";
                    }
                    else
                    {
                        lbl_vipham.Text = "";

                    }
                else
                {
                    lbl_vipham.Text = "";

                }
            }
            else
                Response.Redirect("Default.aspx");
        }
        //private void GetNhanXet_GV()
        //{
        //    if (Session["hs"] != null && Session["nk"] != null && Session["hk"] != null && Session["lop"] != null)
        //    {
        //        DataTable dt = new DataTable();
        //        DataSet ds = cls.Get_NhanXet(Session["hk"].ToString(), Session["nk"].ToString(), Session["hs"].ToString(), Session["lop"].ToString(), 1);
        //        if (ds != null && ds.Tables.Count > 0)
        //            if (ds.Tables[0].Rows.Count > 0)
        //            {
        //                rid_gv.DataSource = ds.Tables[0].DefaultView;
        //                rid_gv.DataBind();
        //            }
        //            else
        //            {
        //                rid_gv.DataSource = dt;
        //                rid_gv.DataBind();

        //            }
        //        else
        //        {
        //            rid_gv.DataSource = dt;
        //            rid_gv.DataBind();

        //        }

        //    }
        //  }

        private void GetNhanXet_PH()
        {
            if (Session["hs"] != null && Session["nk"] != null && Session["hk"] != null && Session["lop"] != null)
            {
                DataTable dt = new DataTable();
                DataSet ds = cls.Get_NhanXet(Session["hk"].ToString(), Session["nk"].ToString(), Session["hs"].ToString(), Session["lop"].ToString(), 0);
                if (ds != null && ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        rid_ph.DataSource = ds.Tables[0].DefaultView;
                        rid_ph.DataBind();
                    }
                    else
                    {
                        rid_ph.DataSource = dt;
                        rid_ph.DataBind();
                    }
                }
                else
                {
                    rid_ph.DataSource = dt;
                    rid_ph.DataBind();
                }

            }
            else
                Response.Redirect("Default.aspx");
        }

        protected void rdo_HK_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Session["hs"] != null && Session["nk"] != null && Session["hk"] != null && Session["lop"] != null && Convert.ToDecimal(Session["Loai"]) == 0)
            {
                string hocsinh = Session["hs"].ToString();
                string nienkhoa = Session["nk"].ToString();
                string hocky = Session["hk"].ToString();
                string lop = Session["lop"].ToString();
                GridColumn columnTemp1 = gvResult.MasterTableView.Columns.FindByUniqueName("CN");
                GridColumn columnTemp2 = gvResult.MasterTableView.Columns.FindByUniqueName("HK1");
                if (rdo_HK.SelectedValue == "0")
                {
                    // gvResult.DataSource = cls.Get_Diem_HS("HK1", nienkhoa, hocsinh, lop);
                    gvResult.DataSource = cls.Get_Diem_HS("HK1", nienkhoa, hocsinh, lop).Tables[0].DefaultView;
                    gvResult.DataBind();
                    columnTemp1.Visible = false;
                    columnTemp2.Visible = false;
                    //load hoc luc hoc sinh
                    DataSet ds_hocluc = cls.Get_HOCLUC_HS(lop, nienkhoa, hocsinh, "HK1");
                    if (ds_hocluc != null && ds_hocluc.Tables.Count > 0)
                        if (ds_hocluc.Tables[0].Rows.Count > 0)
                        {
                            //lblDTB.Text = "DTB: " + ds_hocluc.Tables[0].Rows[0]["DiemTB"].ToString();
                            //lbl_hang.Text = "Hạng: " + ds_hocluc.Tables[0].Rows[0]["Hang"].ToString();
                            //lbl_dntd.Text = "DHTĐ: " + ds_hocluc.Tables[0].Rows[0]["DanhHieuThiDua"].ToString();
                            //lbl_loai.Text = "Học lực: " + ds_hocluc.Tables[0].Rows[0]["hocluc"].ToString();
                            lblhk.Text = ds_hocluc.Tables[0].Rows[0]["hanhkiem"].ToString();
                            lbl_nghi.Text = ds_hocluc.Tables[0].Rows[0]["Nghi"].ToString();
                            lbl_nhanxet.Text = ds_hocluc.Tables[0].Rows[0]["Nhanxet"].ToString();
                            g_hocluc += "<table style=\"background: #2119B2; width: 520px;\"> <tr>";
                            g_hocluc += " <td style=\"width:60px;color:White\">" + "ĐTB: " + ds_hocluc.Tables[0].Rows[0]["DiemTB"].ToString() + "</td>";
                            g_hocluc += " <td style=\"width:100px;color:White\">" + "Học lực: " + ds_hocluc.Tables[0].Rows[0]["hocluc"].ToString() + "</td>";
                            g_hocluc += " <td style=\"width:60px;color:White\">" + "Hạng: " + ds_hocluc.Tables[0].Rows[0]["Hang"].ToString() + "</td>";
                            g_hocluc += " <td style=\"width:80px;color:White\">" + "DHTĐ: " + ds_hocluc.Tables[0].Rows[0]["DanhHieuThiDua"].ToString() + "</td>";
                            g_hocluc += "</tr></table>";
                        }
                        else
                        {
                            //lblDTB.Text = "";
                            //lbl_hang.Text = "";
                            //lbl_dntd.Text = "";
                            //lbl_loai.Text = "";
                            lblhk.Text = "";
                            lbl_nhanxet.Text = "";
                            lbl_nghi.Text = "";
                            g_hocluc = "";
                        }
                    //end
                    Session["hk"] = "HK1";
                    //  GetNhanXet_GV();
                    // GetNhanXet_PH();--
                    g_canam = "";
                    // Get_ViPham(hocky);--
                }
                else
                {
                    Session["hk"] = "HK2";
                    gvResult.DataSource = cls.Get_Diem_HS("HK2", nienkhoa, hocsinh, lop).Tables[0].DefaultView;
                    gvResult.DataBind();
                    columnTemp1.Visible = true;
                    columnTemp2.Visible = true;
                    // load hoc luc hoc sinh
                    DataSet ds_hocluc = cls.Get_HOCLUC_HS(lop, nienkhoa, hocsinh, "HK2");
                    if (ds_hocluc != null && ds_hocluc.Tables.Count > 0)
                        if (ds_hocluc.Tables[0].Rows.Count > 0)
                        {
                            //lblDTB.Text = "DTB: " + ds_hocluc.Tables[0].Rows[0]["DiemTB"].ToString();
                            //lbl_hang.Text = "Hạng: " + ds_hocluc.Tables[0].Rows[0]["Hang"].ToString();
                            //lbl_dntd.Text = "DHTĐ: " + ds_hocluc.Tables[0].Rows[0]["DanhHieuThiDua"].ToString();
                            //lbl_loai.Text = "Học lực: " + ds_hocluc.Tables[0].Rows[0]["hocluc"].ToString();
                            lblhk.Text = ds_hocluc.Tables[0].Rows[0]["hanhkiem"].ToString();
                            lbl_nghi.Text = ds_hocluc.Tables[0].Rows[0]["Nghi"].ToString();
                            lbl_nhanxet.Text = ds_hocluc.Tables[0].Rows[0]["Nhanxet"].ToString();
                            g_hocluc += "<table style=\"background: #2119B2; width: 580px;\"> <tr>";
                            g_hocluc += " <td style=\"width:60px;color:White\">" + "ĐTB: " + ds_hocluc.Tables[0].Rows[0]["DiemTB"].ToString() + "</td>";
                            g_hocluc += " <td style=\"width:100px;color:White\">" + "Học lực: " + ds_hocluc.Tables[0].Rows[0]["hocluc"].ToString() + "</td>";
                            g_hocluc += " <td style=\"width:60px;color:White\">" + "Hạng: " + ds_hocluc.Tables[0].Rows[0]["Hang"].ToString() + "</td>";
                            g_hocluc += " <td style=\"width:80px;color:White\">" + "DHTĐ: " + ds_hocluc.Tables[0].Rows[0]["DanhHieuThiDua"].ToString() + "</td>";
                            g_hocluc += "</tr></table>";
                        }
                        else
                        {
                            //lblDTB.Text = "";
                            //lbl_hang.Text = "";
                            //lbl_dntd.Text = "";
                            //lbl_loai.Text = "";
                            lblhk.Text = "";
                            lbl_nghi.Text = "";
                            lbl_nhanxet.Text = "";
                            g_hocluc = "";
                        }
                    //end

                    // GetNhanXet_GV();
                    //   GetNhanXet_PH();--
                    //   Get_ViPham(hocky);--
                    string d1 = "";
                    string d2 = "";
                    string d3 = "";
                    string hl1 = "";
                    string hl2 = "";
                    string hl3 = "";
                    string hk1 = "";
                    string hk2 = "";
                    string hk3 = "";
                    string hang1 = "";
                    string hang2 = "";
                    string hang3 = "";
                    string nghi1 = "";
                    string nghi2 = "";
                    string nghi3 = "";
                    string dhtd1 = "";
                    string dhtd2 = "";
                    string dhtd3 = "";
                    string kq = "";
                    DataSet ds_canam = cls.GET_KQ_CANAM(lop, nienkhoa, hocsinh);
                    if (ds_canam != null && ds_canam.Tables.Count > 0)
                        if (ds_canam.Tables[0].Rows.Count > 0)
                        {
                            for (int i = 0; i < ds_canam.Tables[0].Rows.Count; i++)
                            {
                                if (i == 0)
                                {
                                    d1 = ds_canam.Tables[0].Rows[i]["DiemTB"].ToString();
                                    hl1 = ds_canam.Tables[0].Rows[i]["TenHocLuc"].ToString();
                                    hk1 = ds_canam.Tables[0].Rows[i]["TenHanhKiem"].ToString();
                                    hang1 = ds_canam.Tables[0].Rows[i]["Hang"].ToString();
                                    nghi1 = ds_canam.Tables[0].Rows[i]["Nghi"].ToString();
                                    dhtd1 = ds_canam.Tables[0].Rows[i]["DanhHieuThiDua"].ToString();
                                }
                                else if (i == 1)
                                {
                                    d2 = ds_canam.Tables[0].Rows[i]["DiemTB"].ToString();
                                    hl2 = ds_canam.Tables[0].Rows[i]["TenHocLuc"].ToString();
                                    hk2 = ds_canam.Tables[0].Rows[i]["TenHanhKiem"].ToString();
                                    hang2 = ds_canam.Tables[0].Rows[i]["Hang"].ToString();
                                    nghi2 = ds_canam.Tables[0].Rows[i]["Nghi"].ToString();
                                    dhtd2 = ds_canam.Tables[0].Rows[i]["DanhHieuThiDua"].ToString();
                                }
                                else
                                {
                                    d3 = ds_canam.Tables[0].Rows[i]["DiemTB"].ToString();
                                    hl3 = ds_canam.Tables[0].Rows[i]["TenHocLuc"].ToString();
                                    hk3 = ds_canam.Tables[0].Rows[i]["TenHanhKiem"].ToString();
                                    hang3 = ds_canam.Tables[0].Rows[i]["Hang"].ToString();
                                    nghi3 = ds_canam.Tables[0].Rows[i]["Nghi"].ToString();
                                    dhtd3 = ds_canam.Tables[0].Rows[i]["DanhHieuThiDua"].ToString();
                                }

                            }
                        }
                    g_canam = "<table cellpadding=\"0\" cellspacing=\"0\" class=\"style10\" border=\"1\"frame=\"border\" style=\"border:1px thin #0066FF; border-collapse: collapse;\">";
                    g_canam += "<tr><td style=\"background-color: #0066FF\"></td><td class=\"style14\">Học kỳ 1</td><td class=\"style14\">Học kỳ 2</td><td class=\"style14\">Cả năm</td></tr>";
                    g_canam += " <tr><td class=\"style16\">Điểm bình quân</td><td class=\"style16\">" + d1 + "</td><td class=\"style16\">" + d2 + "</td><td class=\"style16\">" + d3 + "</td></tr>";
                    g_canam += " <tr><td class=\"style16\">Học Lực</td><td class=\"style16\">" + hl1 + "</td><td class=\"style16\">" + hl2 + "</td><td class=\"style16\">" + hl3 + "</td></tr>";
                    g_canam += " <tr><td class=\"style16\">Hạnh kiểm</td><td class=\"style16\">" + hk1 + "</td><td class=\"style16\">" + hk2 + "</td><td class=\"style16\">" + hk3 + "</td></tr>";
                    g_canam += " <tr><td class=\"style16\">Hạng</td><td class=\"style16\">" + hang1 + "</td><td class=\"style16\">" + hang2 + "</td><td class=\"style16\">" + hang3 + "</td></tr>";
                    g_canam += " <tr><td class=\"style16\">Nghỉ (CP/KP)</td><td class=\"style16\">" + nghi1 + "</td><td class=\"style16\">" + nghi2 + "</td><td class=\"style16\">" + nghi3 + "</td></tr>";
                    g_canam += " <tr><td class=\"style16\">DHTĐ</td><td class=\"style16\">" + dhtd1 + "</td><td class=\"style16\">" + dhtd2 + "</td><td class=\"style16\">" + dhtd3 + "</td></tr>";
                    DataSet ds_checkthilai = cls.Check_ThiLai(nienkhoa, hocsinh);
                    if (ds_checkthilai != null && ds_checkthilai.Tables[0].Rows.Count > 0)
                    {
                        kq = "Thi lại";
                        g_thilai += "<p> <b>Danh sách các môn yêu cầu thi lại, rèn luyện hè</b></p><br/>";
                        g_thilai += "<table cellpadding=\"1\" class=\"style10\"  border=\"1\"frame=\"border\" cellspacing=\"1\" style=\"border:1px solid; border-collapse: collapse;\">";
                        g_thilai += "<tr><td>STT</td><td>Môn thi lại</td><td>Điểm TB trước khi thi</td><td>Điểm TB sau khi thi lại</td></tr>";
                        for (int i = 0; i < ds_checkthilai.Tables[0].Rows.Count; i++)
                        {
                            g_thilai += "<tr><td>" + ds_checkthilai.Tables[0].Rows[i]["STT"] + "</td>" +
                                        "<td>" + ds_checkthilai.Tables[0].Rows[i]["TenMonHoc"] + "</td>" +
                                        "<td>" + ds_checkthilai.Tables[0].Rows[i]["TB1"] + "</td>" +
                                         "<td>" + ds_checkthilai.Tables[0].Rows[i]["TB2"] + "</td></tr>";
                        }
                        g_thilai += "</table>";
                        g_ketquarenluyen += "<p> <b>Kết quả sau khi thi lại, rèn luyện hè</b></p><br/>";
                        g_ketquarenluyen += "<table cellpadding=\"1\" class=\"style10\"  border=\"1\"frame=\"border\" cellspacing=\"1\" style=\"border:1px solid; border-collapse: collapse;\">";
                        g_ketquarenluyen += "<tr><td>STT</td><td>Nội dung</td><td>Kết quả</td><td>Đề nghị</td></tr>";
                        g_ketquarenluyen += "<tr><td>1</td><td>Hạnh kiểm trong năm</td><td>" + hk2 + "</td><td>";
                        if (hk2 == "Yếu")
                            g_ketquarenluyen += "Rèn luyện hè</td></tr>";
                        else
                            g_ketquarenluyen += "</td></tr>";
                        g_ketquarenluyen += "<tr><td>2</td><td>Hạnh kiểm sau khi đã rèn luyện</td><td>" + hk3 + "</td><td></td></tr>";

                        g_ketquarenluyen += "<tr><td>3</td><td>Kết quả sau khi rèn luyện</td><td></td><td>";
                        if (hk3 == "Yếu")
                            g_ketquarenluyen += "Lưu ban</td></tr>";
                        else
                            g_ketquarenluyen += "Được lên lớp</td></tr>";
                        g_ketquarenluyen += "<tr><td>4</td><td>Điểm trung bình môn trong năm</td><td>" + d2 + "</td><td>" + "Thi lại" + "</td></tr>";
                        g_ketquarenluyen += "<tr><td>5</td><td>Điểm trung bình môn sau khi thi lại</td><td>" + d3 + "</td><td></td></tr>";
                        g_ketquarenluyen += "<tr><td>6</td><td>Kết quả sau khi thi lại</td><td></td><td>"; //+hl3 == "Yếu" ? "Lưu ban" : "Được lên lớp" + "</td>";
                        if (hl3 == "Yếu")
                            g_ketquarenluyen += "Lưu ban</td>";
                        else
                            g_ketquarenluyen += "Được lên lớp</td>";
                        g_ketquarenluyen += "</tr></table>";

                    }
                    else
                        kq = "Lên lớp";
                    if (hl3 == "Yếu")
                        kq = "Lưu ban";
                    else if (hl3 == "")
                        kq = "";
                    else
                    {


                    }
                    g_canam += "<tr><td class=\"style16\">Kết quả</td><td class=\"style15\" colspan=\"3\">" + kq + "</td></tr></table>";
                }

            }

            else
            {
                Response.Redirect("Default.aspx");
            }
        }

        protected void btn_gopy_PH_Click(object sender, EventArgs e)
        {
            if (Session["hs"] != null && Session["nk"] != null && Session["hk"] != null && Session["lop"] != null)
            {
                if (txtghichu_PH.Text != string.Empty)
                {
                    bool insert = cls.Insert_NhanXet(Session["lop"].ToString(), Session["nk"].ToString(), Session["hk"].ToString(), DateTime.Now, txtghichu_PH.Text, 0, Session["hs"].ToString());
                    GetNhanXet_PH();
                    // Page.ClientScript.RegisterStartupScript(this.GetType(), "Thông Báo", "<script>alert('Góp ý thành công!')</script>");
                    txtghichu_PH.Text = "";
                }
            }
            else
            {
                Response.Redirect("Default.aspx");
            }
        }


    }
}